package com.zhaoxi.project.wms.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.project.wms.domain.WmsProductStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.zhaoxi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhaoxi.project.wms.mapper.WmsProductStockCheckMapper;
import com.zhaoxi.project.wms.domain.WmsProductStockCheck;

/**
 * 仓储盘点Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-08-25
 */
@Service
public class WmsProductStockCheckServiceImpl {
    @Autowired
    private WmsProductStockCheckMapper wmsProductStockCheckMapper;

    /**
     * 查询仓储盘点
     *
     * @param id 仓储盘点主键
     * @return 仓储盘点
     */

    public WmsProductStockCheck selectWmsProductStockCheckById(Long id) {
        WmsProductStockCheck  wmsProductStockCheck = wmsProductStockCheckMapper.selectWmsProductStockCheckById(id);
        List<WmsProductStockCheck> list = wmsProductStockCheckMapper.selectWmsProductStockCheckLineList(wmsProductStockCheck);
        wmsProductStockCheck.setWmsProductStockCheckLineList(list);
        return wmsProductStockCheck;
    }

    /**
     * 查询仓储盘点列表
     *
     * @param wmsProductStockCheck 仓储盘点
     * @return 仓储盘点
     */

    public List<WmsProductStockCheck> selectWmsProductStockCheckList(WmsProductStockCheck wmsProductStockCheck) {
        return wmsProductStockCheckMapper.selectWmsProductStockCheckList(wmsProductStockCheck);
    }

    /**
     * 新增仓储盘点
     *
     * @param wmsProductStockCheck 仓储盘点
     * @return 结果
     */
    @Transactional

    public int insertWmsProductStockCheck(WmsProductStockCheck wmsProductStockCheck) {
        wmsProductStockCheck.setCreateTime(DateUtils.getNowDate());
        int rows = wmsProductStockCheckMapper.insertWmsProductStockCheck(wmsProductStockCheck);
        List<WmsProductStockCheck> stockCheckLineList = wmsProductStockCheck.getWmsProductStockCheckLineList();
        if (stockCheckLineList != null && !stockCheckLineList.isEmpty()) {
            for (WmsProductStockCheck stockCheckLine : stockCheckLineList) {
                String productCode = stockCheckLine.getProductCode();
                BigDecimal quantity = stockCheckLine.getQuantity();
                insertWmsProductStockCheckLine(wmsProductStockCheck);
                updateWmsProductStock(productCode, quantity);
            }
        }
        return rows;
    }

    /**
     * 修改仓储盘点
     *
     * @param wmsProductStockCheck 仓储盘点
     * @return 结果
     */
    @Transactional

    public int updateWmsProductStockCheck(WmsProductStockCheck wmsProductStockCheck) {
        wmsProductStockCheck.setUpdateTime(DateUtils.getNowDate());
        wmsProductStockCheckMapper.deleteWmsProductStockCheckLineById(wmsProductStockCheck.getId());
        int rows = wmsProductStockCheckMapper.updateWmsProductStockCheck(wmsProductStockCheck);
        List<WmsProductStockCheck> stockCheckList = wmsProductStockCheck.getWmsProductStockCheckLineList();
        if (stockCheckList != null && !stockCheckList.isEmpty()) {
            for (WmsProductStockCheck stockCheckLine : stockCheckList) {
                String productCode = stockCheckLine.getProductCode();
                BigDecimal quantity = stockCheckLine.getQuantity();
                insertWmsProductStockCheckLine(wmsProductStockCheck);
                updateWmsProductStock(productCode, quantity);
            }
        }
        return rows;
    }

    /**
     * 批量删除仓储盘点
     *
     * @param ids 需要删除的仓储盘点主键
     * @return 结果
     */
    @Transactional

    public int deleteWmsProductStockCheckByIds(Long[] ids) {
        wmsProductStockCheckMapper.deleteWmsProductStockCheckLineByIds(ids);
        return wmsProductStockCheckMapper.deleteWmsProductStockCheckByIds(ids);
    }

    /**
     * 删除仓储盘点信息
     *
     * @param id 仓储盘点主键
     * @return 结果
     */
    @Transactional

    public int deleteWmsProductStockCheckById(Long id) {
        wmsProductStockCheckMapper.deleteWmsProductStockCheckLineById(id);
        return wmsProductStockCheckMapper.deleteWmsProductStockCheckById(id);
    }


public  void  updateWmsProductStock(String productCode, BigDecimal quantity){
    WmsProductStock wmsProductStock = new WmsProductStock();
    wmsProductStock.setProductCode(productCode);
    wmsProductStock.setQuantity(quantity);
    // 调用Mapper执行更新操作
    wmsProductStockCheckMapper.updateWmsProductStock(wmsProductStock);
}

    /** 新增库存盘点信息
     *
     * @param wmsProductStockCheck 仓储盘点对象*/
    public void insertWmsProductStockCheckLine(WmsProductStockCheck wmsProductStockCheck) {
        List<WmsProductStockCheck> wmsProductStockCheckLineList = wmsProductStockCheck.getWmsProductStockCheckLineList();
        Long id = wmsProductStockCheck.getId();
        if (StringUtils.isNotNull(wmsProductStockCheckLineList)) {
            List<WmsProductStockCheck> list = new ArrayList<WmsProductStockCheck>();
            for (WmsProductStockCheck wmsProductStockCheckLine : wmsProductStockCheckLineList) {
                wmsProductStockCheckLine.setId(id);
                list.add(wmsProductStockCheckLine);
            }
            if (list.size() > 0) {
                wmsProductStockCheckMapper.batchWmsProductStockCheckLine(list);
            }
        }
    }
    public int updateWmsProductStockCheckLine(WmsProductStockCheck wmsProductStockCheck){
        wmsProductStockCheck.setUpdateTime(DateUtils.getNowDate());
        return wmsProductStockCheckMapper.updateWmsProductStockCheckLine(wmsProductStockCheck);
    }
}